<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	$snsid = $this->session('snsid');
	$type = intval($_RQ['type']);
	set_html('title',$type==1?'申请成为车主':'申请成为专线单位');
	$member=$this->model('member')->getinfo();
	$apply=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'id');
	if($apply){
		$return['apply']=$apply;
		$return['applystatus']=$this->getcolumn('apply',array('mid'=>$_SESSION['uid']),'status');
	}else{
		//判断是否满足申请车主条件
		$can_apply=true;
		$return['fromnow']=pdo_count('jy_wuliu_member',array('formuid'=>$_SESSION['uid']),'id');
		if($type==1 && $this->setting['chezhu_becom']){
			$return['chezhu_fromnum']=$this->setting['chezhu_fromnum'];
			if($return['chezhu_fromnum']>$return['fromnow']){
				$can_apply=false;
			}
		}
		if($type==2 && $this->setting['zhuanxian_becom']){
			$return['zhuanxian_fromnum']=$this->setting['zhuanxian_fromnum'];
			if($return['zhuanxian_fromnum']>$return['fromnow']){
				$can_apply=false;
			}
		}
	}
	$return['can_apply']=$can_apply;
	$return['gamexieyi']=$this->plugin_setting('gamexieyi');
	$return['systemxieyi']=$this->plugin_setting('systemxieyi');
	$member['type']=$type;
	$member['pcd']=$member['pcd']?json_decode($member['pcd'],true):array();
	$return['member']=$member;
	$htmltitle = $this->setting['title'];
	$share_title = $this->setting['share_desc'];
	$share_image = $this->setting['share_image'];
	$snsinfo = pdo_get('jy_wuliu_sns',array('uniacid'=>$_SESSION['uniacid'],'id'=>$snsid));
	$return['sharedata']=array(
		'title'=>$snsinfo['title']?:'物流圈',
		'desc'=>$share_title?:'大家好！我发现一个不错的货运物流信息平台，进入查看一下！',
		'link'=>custom_url('jy_wuliu/index//snsid='.$snsid.'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true),
		'imgUrl'=>tomedia($share_image?:'../addons/jy_wuliu/resource/images/ico-wuliu.png'),
	);
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['title']){
		exi('请输入商户名称！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	if($post['type']==1 && !$post['chepai']){
		exi('请输入车牌！','error');
	}
	if($post['type']==1 && !$post['cheimg']){
		exi('请输入车照片！','error');
	}
	if($post['type']==2 && !$post['zhizhao']){
		exi('请输入店营业执照！','error');
	}
	if($post['type']==2 && !$post['zhuanxianimg']){
		exi('请输入店照片！','error');
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	model('core/member')->member_update($_SESSION['uid'],$member_update);
	$update=array(
		'title'=>trim($post['title']),
		'lat_lng'=>ijson_encode($post['lat_lng']),
		'address'=>trim($post['address']),
		'chepai'=>trim($post['chepai']),
		'zhizhao'=>trim($post['zhizhao']),
		'cheimg'=>trim($post['cheimg']),
		'logo'=>trim($post['cheimg'])?:trim($post['zhuanxianimg']),
		'zhuanxianimg'=>trim($post['zhuanxianimg']),
		'type'=>trim($post['type']),
	);
	pdo_update('jy_wuliu_member',$update,array('mid'=>$_SESSION['uid']));
	$this->model('member')->apply_merch($update['type']);
	if($post['type']==1){
		if($this->setting['ischezhu_shenhe']){
			exi('申请车主成功！请耐心等待管理员审核！','','main');
		}else{
			exi('申请车主成功！','','jy_wuliu/myquan/');
		}
	}
	if($post['type']==2){
		if($this->setting['iszhuanxian_shenhe']){
			exi('申请专线成功！请耐心等待管理员审核！','','main');
		}else{
			exi('申请专线成功！','','jy_wuliu/myquan/');
		}
	}
	
}